﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace MendeleyClient.Phone
{
    public class PublicationRecord
    {
        public string name;
        public string value;

        public PublicationRecord(string name, string value)
        {
            this.name = name;
            this.value = value;
        }

        static public List<PublicationRecord> Parse(string str)
        {
            var records = new List<PublicationRecord>();

            for (; ; )
            {
                var startIndx = str.IndexOf("{");
                var endIndx = str.IndexOf("}");
                if (startIndx < 0 || endIndx <= startIndx)
                {
                    break;
                }
                var recordStr = str.Substring(startIndx, endIndx);
                recordStr = recordStr.Replace("{", "");
                recordStr = recordStr.Replace("}", "");

                if (recordStr.Length > 0)
                {
                    recordStr = recordStr.Replace("\"name\":", "");
                    recordStr = recordStr.Replace("\"value\":", "");
                    recordStr = recordStr.Replace("\"", "");

                    var kv = recordStr.Split(',');
                    var record = new PublicationRecord(kv[0], kv[1]);
                    records.Add(record);
                }
                else
                {
                    break;
                }
                str = str.Substring(endIndx + 1);
            }

            return records;
        }
    }
}
